<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.news_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/service/news";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide:true,
              display:false
            },
            {
              label: "文章标题",
              prop: "title",
              search: true,
              overHidden:true,
              span:24,
              rules: [{
                required: true,
                message: "请输入标题",
                trigger: "blur"
              }],
              maxlength:50,
              showWordLimit:true
            },
            {
              label: "学校名称",
              prop: "schoolId",
              type: "select",
              search:true,
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入学校名称",
                trigger: "blur"
              }],
              dicUrl: "/api/school-service/school/select",
              props: {
                label: "xxName",
                value: "id"
              },
            },
            {
              label: "作者",
              prop: "author",
              search: true,
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入作者",
                trigger: "blur"
              }],
              maxlength:50,
              showWordLimit:true
            },
            {
              label: "来源",
              prop: "source",
              search: true,
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入来源",
                trigger: "blur"
              }],
              maxlength:50,
              showWordLimit:true
            },
            {
              label: "内容",
              prop: "content",
              span:24,
              component: "avueUeditor",
              //wangEditor编辑的配置
              customConfig: {
                lineHeights: ['1', '1.15', '1.6', '2', '2.5', '3']
              },
              //是否启用oss(qiniu/ali)
              oss:'',
              headers:{},
              data:{},
              propsHttp: {
                home:'',
                url:'',
                res: ''
              },
              //普通图片上传
              action: "",
              //七牛云oss配置
              qiniu: {
                AK: "",
                SK: "",
                scope: "",
                url: "",
                deadline: 1
              },
              //阿里云oss配置
              ali: {
                region: "",
                endpoint: "",
                accessKeyId: "",
                accessKeySecret: "",
                bucket: ""
              },
              hide: true
            },
            {
              label: "发布类型",
              prop: "publishType",
              search: true,
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入发布类型",
                trigger: "blur"
              }],
              type: 'select',
              dicUrl: "/api/blade-system/dict/dictionary?code=FABULEIXING",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
            },
            {
              label: "发布时间",
              search: true,
              prop: "publishTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入发布时间",
                trigger: "blur"
              }]
            },

            {
              label: "显示时间",
              prop: "showTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入显示时间",
                trigger: "blur"
              }]
            },
            {
              label: "封面图",
              prop: "pic",
              rules: [{
                required: true,
                message: "请输入封面图",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 24,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "状态",
              search: true,
              prop: "newsStatus",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入状态",
                trigger: "blur"
              }],
              type: 'select',
              dicUrl: "/api/blade-system/dict/dictionary?code=WENZHANGZHUANGTAI",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.news_add, false),
          viewBtn: this.vaildData(this.permission.news_view, false),
          delBtn: this.vaildData(this.permission.news_delete, false),
          editBtn: this.vaildData(this.permission.news_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
        const school = this.findObject(this.option.column, "schoolId");
        school.hide = true;
      }
    }
  };
</script>

<style>
</style>
